package org.example.zuoye.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.zuoye.entity.Schedules;
import org.example.zuoye.mapper.SchedulesMapper;
import org.example.zuoye.service.SchedulesService;
import org.example.zuoye.vo.SchedulesByDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author xinchen
 * @Date: 2024/08/14/ 21:21
 * @Description
 */
@Service
public class SchedulesServiceImpl implements SchedulesService {

    @Autowired
    private SchedulesMapper schedulesMapper;
    /**
     * 新增操作
     * @return
     */
    @Override
    public PageInfo search(Integer page, Integer limit, Schedules schedules) {
        try(Page __ = PageHelper.startPage(page, limit)) {
            List<Schedules> list = schedulesMapper.search(schedules);
            return new PageInfo(list);
        }

    }

    /**
     * 医生放号
     * @param schedules
     * @return
     */
    @Override
    public int add(Schedules schedules) throws Exception {
        // 校验开始时间不能晚于结束时间
        if (schedules.getStartTime().compareTo(schedules.getEndTime()) > 0) {
            throw new Exception("开始时间不能晚于结束时间");
        }

        // 校验面诊时间不得晚于预约时间
        if (schedules.getBookDate().compareTo(schedules.getEndTime()) < 0) {
            throw new Exception("预约时间不得晚于面诊时间");
        }
        schedules.setAvailableSlots(schedules.getTotalSlots());
        return schedulesMapper.insertSelective(schedules);
    }

    @Override
    public int edit(Schedules schedules) throws Exception {
        // 校验开始时间不能晚于结束时间
        if (schedules.getStartTime().compareTo(schedules.getEndTime()) > 0) {
            throw new Exception("开始时间不能晚于结束时间");
        }

        // 校验面诊时间不得晚于预约时间
        if (schedules.getBookDate().compareTo(schedules.getEndTime()) < 0) {
            throw new Exception("预约时间不得晚于面诊时间");
        }
        return schedulesMapper.updateByPrimaryKeySelective(schedules);
    }

    @Override
    public int remove(Integer[] ids) throws Exception {
        return schedulesMapper.deleteByIds(ids);
    }

    /**
     * 获取今日可以预约的医生
     * @return
     */
    @Override
    public List<SchedulesByDay> findToday() {
        return schedulesMapper.findToday();
    }

    /**
     * 医生放好 edit
     */
}
